package com.qtyx.nms.ui.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.ldd.base.ui.adapter.BaseRVAdapter
import com.ldd.base.ui.adapter.BaseRVVH
import com.qtyx.nms.R
import com.qtyx.nms.mvp.model.bean.NurseBean
import com.qtyx.nms.ui.activity.BedListActivity
import com.qtyx.nms.ui.activity.NurseBedListActivity
import com.qtyx.nms.util.DisplayUtil
import com.qtyx.nms.util.IntentKey

/**
 * 护理适配器
 */
class NurseAdapter(mContext: Context, list: List<NurseBean.Data>, private val spanCount:Int) :BaseRVAdapter<NurseBean.Data>(mContext, list) {

    override fun getLayoutId(viewType: Int)= R.layout.item_nurse

    /**护理项目类型：1、一般，2、特殊**/
    var nurseItemType=1

    override fun convert(vh: BaseRVVH, position: Int, data: NurseBean.Data) {
        val clMain=vh.getView<ConstraintLayout>(R.id.cl_item_nurse_main)
        val llMainBg=vh.getView<LinearLayout>(R.id.ll_item_nurse_main)
        val clName=vh.getView<ConstraintLayout>(R.id.cl_item_nurse_name)
        val tvName=vh.getView<TextView>(R.id.tv_item_nurse_name)
        val tvBedCount=vh.getView<TextView>(R.id.tv_item_nurse_bed_count)
        val rvList=vh.getView<RecyclerView>(R.id.rv_item_nurse_list)
        val tvBedEmptyHint=vh.getView<TextView>(R.id.tv_item_nurse_bed_empty_hint)

        val padding= DisplayUtil.dp2px(1f)
        if(position%spanCount==0&&spanCount!=1){
            clMain.setPadding(0,0,0,0)
        }else{
            clMain.setPadding(padding,0,0,padding)
        }

        if(1==nurseItemType){
            llMainBg.setBackgroundResource(R.drawable.shape_homepage_general_nurse_bg)
        }else{
            llMainBg.setBackgroundResource(R.drawable.shape_homepage_special_nurse_bg)
        }


        if(data.nursprogName.isEmpty()){
            tvName.text= ""
        }else{
            tvName.text= data.nursprogName
            tvName.textSize=if(data.nursprogName.length>4) 12f else 16f

            if(data.titleColor.isNotEmpty()){
                tvName.setTextColor(Color.parseColor(data.titleColor))
            }
        }

        //床位数据为空，隐藏列表、显示为空提示
        if(data.sqlData.isNullOrEmpty()){
            tvBedCount.visibility=View.GONE
            rvList.visibility=View.GONE
            //项目不为空，说明有数据，否则为补全的数据
            if(data.nursprogName.isEmpty()){
                tvBedEmptyHint.visibility = View.GONE
            }else{
                tvBedEmptyHint.visibility = View.VISIBLE
            }
        }else{
            tvBedCount.visibility=View.VISIBLE
            rvList.visibility=View.VISIBLE
            tvBedEmptyHint.visibility=View.GONE
            //床位数
            tvBedCount.text=data.sqlData.size.toString()
            //床位列表
            rvList.layoutManager=StaggeredGridLayoutManager(1,RecyclerView.HORIZONTAL)

            val color=data.contentColor
            val adapter=NurseChildAdapter(mContext,data.sqlData,color)
            rvList.adapter=adapter
            //项目名称
            clName.setOnClickListener {
                val mClass:Class<*> = if("3"==data.nursprogType){
                    BedListActivity::class.java
                }else {
                    NurseBedListActivity::class.java
                }
                val mIntent = Intent(mContext, mClass).apply {
                    putExtra(IntentKey.nurseBedInfo, data)
                }
                mContext.startActivity(mIntent)
            }
            //点击床位列表某项进入
            adapter.onItemClickListener=object:NurseChildAdapter.OnItemClickListener{
                override fun onItemClick(position: Int) {
                    val mClass:Class<*> = if("3"==data.nursprogType){
                        BedListActivity::class.java
                    }else {
                        NurseBedListActivity::class.java
                    }
                    val mIntent = Intent(mContext, mClass).apply {
                        putExtra(IntentKey.nurseBedInfo, data)
                        putExtra(IntentKey.nurseBedSelectPosition, position)
                    }
                    mContext.startActivity(mIntent)
                }
            }
        }


    }

}